{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2f3324e-4b2c-42f3-8fd5-53a5a341c42f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "from osgeo import gdal\n",
    "import shapely\n",
    "import database\n",
    "import import_img\n",
    "from imgview import ImgView"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff9a4c5d-c09c-43f7-9f2a-8f34c6c22cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "db = database.Database('data/imagery.db')\n",
    "A_path = 'data/DZB1211-500049L007001.tif'\n",
    "B_path = 'data/D3C1208-200141A020_h.tif'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c01f74ae-5552-42f0-98f5-8ddbaaa3acba",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "fidA, iidA = import_img.import_img(db, A_path)\n",
    "fidB, iidB = import_img.import_img(db, B_path)\n",
    "dsA = gdal.Open(A_path, gdal.GA_ReadOnly)\n",
    "dsB = gdal.Open(B_path, gdal.GA_ReadOnly)\n",
    "ivA = ImgView(dsA.GetRasterBand(1))\n",
    "ivB = ImgView(dsB.GetRasterBand(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ec1eec9-36e7-405b-bee6-5e2295b61ad7",
   "metadata": {},
   "outputs": [],
   "source": [
    "result_in_db = db.get_match(iidA, iidB)\n",
    "H, area_B_in_A_wkt, n_points, last_update = result_in_db\n",
    "area_B_in_A = shapely.from_wkt(area_B_in_A_wkt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b5bbf3b-0b37-4a9a-bad7-77ce9c7fe39c",
   "metadata": {},
   "outputs": [],
   "source": [
    "box_B = shapely.box(0, 0, ivB.shape[1], ivB.shape[0])\n",
    "area_B_in_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd5139d6-e60b-4ec2-bcc7-7ab7fef9937b",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor=db.conn.cursor()\n",
    "cursor.execute(\n",
    "    \"SELECT b_imgid, transform, ST_AsText(area_b_in_a), n_points, last_update \"\n",
    "    \"FROM matchs \"\n",
    "    \"WHERE a_imgid=?;\",\n",
    "    (iidA,)\n",
    ")\n",
    "poly_lst = []\n",
    "for b_imgid, transform, area_b_in_a, n_points, last_update in cursor:\n",
    "    poly_lst.append(shapely.from_wkt(area_b_in_a))\n",
    "print(poly_lst)\n",
    "shapely.multipolygons(poly_lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "855c0265-e074-4422-805a-c9cb01c036e7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  },
  "vscode": {
   "interpreter": {
    "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
